package com.home.model;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity(name="Folder")
@Table(name="FOLDER")
public class Folder implements Serializable{
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name="folder_id")
   private Long id;
   
   @Column(name="folder_name")
   private String folderName;
   
   @ManyToOne(targetEntity = User.class, fetch = FetchType.EAGER)
   @JoinColumn(name = "user_id", nullable = false)
   private User owner;
   
   @ManyToMany()
   private List<Email> emails;
   
   public Folder ()
   {
      // Do nothing.
   }

   /**
    * @return the id
    */
   public Long getId() {
      return id;
   }

   /**
    * @param id the id to set
    */
   public void setId(Long id) {
      this.id = id;
   }

   /**
    * @return the folderName
    */
   public String getFolderName() {
      return folderName;
   }

   /**
    * @param folderName the folderName to set
    */
   public void setFolderName(String folderName) {
      this.folderName = folderName;
   }

   /**
    * @return the owner
    */
   public User getOwner() {
      return owner;
   }

   /**
    * @param owner the owner to set
    */
   public void setOwner(User owner) {
      this.owner = owner;
   }
   
   @Override
   public String toString()
   {
      return this.folderName;
   }
   
   @Override
   public int hashCode()
   {
      return 11*this.folderName.hashCode();
   }
   
   @Override
   public boolean equals(Object obj)
   {
      if (null == obj)
         return false;
      if (obj instanceof Folder)
      {
         Folder comparedFolder = (Folder) obj;
         if (this.id == comparedFolder.getId())
            return true;
         if (this.folderName.equals(comparedFolder.getFolderName())
                 && this.owner.equals(comparedFolder.getOwner()))
            return true;
      }
      else 
         return false;
      return false;
   }
}
